// pages/contribute/contribute.js
const customRequst = require('../../common/customRequst');
let app = getApp();
Page({
  data: {
    periId: '51',
    statement: '', // 免责声明
    Formtoken: '',
    checked: false,
    userInfo: {},
    periInfo: {},
    isSubmit: false,
    isShow: false,
    uploadFileObj: {
      url: '',
      filename: '',
      fullurl: '',
      isSuccess: false
    },
  },
  onLoad: function (options) {
    let that = this
    if (options.id) {
      that.setData({
        periId: options.id
      })
    }
    //获取投稿书刊信息
    customRequst.postRequest('api/contribution/peridetail', {
      id: that.data.periId
    }).then(function (res) {
      if (res.code == 1) {
        that.setData({
          periInfo: res.data
        })
      } else {
        that.isSubmit = true;
        wx.showToast({
          title: res.msg,
        });
      }
    })
    //初始化表单token
    customRequst.postRequest('api/common/get_form_token').then(res => {
      if (res.code == 1) {
        that.setData({
          Formtoken: res.data.token
        })
      }
    })
  },

  onShow: function () {
    let that = this

    //监听是否上传了文件
    let gUploadFilObj = app.globalData.uploadFileObj;
    if (gUploadFilObj) {
      let uploadFileObj = that.data.uploadFileObj;
      uploadFileObj.url = gUploadFilObj.url;
      uploadFileObj.filename = gUploadFilObj.filename;
      uploadFileObj.fullurl = gUploadFilObj.fullurl;
      uploadFileObj.isSuccess = true;
      that.setData({
        uploadFileObj: uploadFileObj
      })
    }
  },

  //监听页面销毁
  onUnload: function () {
    //清理文件
    app.globalData.uploadFileObj = "";
  },

  //h5上传文件
  uploadFile: function () {
    let h5UploadUrl = app.globalData.workAipDomain + '/api/webupload/index?pages=/pages/contribute/contribute';
    h5UploadUrl = h5UploadUrl.replace(/http:/, 'https:')
    wx.navigateTo({
      url: '/pages/contribute/uploadfile/index?url=' + h5UploadUrl,
    })
  },
  //删除文件
  deleteFile: function () {
    let that = this;
    let uploadFileObj = that.data.uploadFileObj;
    uploadFileObj = {
        url: '',
        filename: '',
        fullurl: '',
        isSuccess: false
      },
      that.setData({
        uploadFileObj: uploadFileObj
      });
  },
  //标记已读
  changeRadio(e) {
    if (e.detail == true) {
      this.setData({
        checked: e.detail,
        statement: 'on'
      })
    } else {
      this.setData({
        checked: e.detail,
        statement: 'off'
      })
    }
  },
  openLoading() {
    wx.navigateTo({
      url: '/pages/xieyi/xieyi'
    })
  },

  //数据提交
  formSubmit: function (e) {

    let that = this
    if (that.data.checked == false) {
      wx.showToast({
        title: '请先阅读投稿须知',
        icon: 'none'
      })
      return;
    }
    if (that.data.isSubmit) {
      wx.showToast({
        title: '请勿重复提交',
        icon: 'none'
      })
      return;
    }
    if (!that.data.uploadFileObj.url) {
      wx.showToast({
        title: '请上传文件',
        icon: 'none'
      })
      return;
    }

    let works_name = e.detail.value.works_name;
    if (!works_name) {
      wx.showToast({
        title: '文章名称不能为空',
        icon: 'none'
      })
      return;
    }
    let author = e.detail.value.author;
    if (!author) {
      wx.showToast({
        title: '作者不能为空',
        icon: 'none'
      })
      return;
    }
    let email = '';
    let emailPwd = '';
    if (that.data.periInfo.types_id != 1) {
      email = e.detail.value.email;
      if (!emaill) {
        wx.showToast({
          title: '邮箱不能为空',
          icon: 'none'
        })
        return;
      }
      emailPwd = e.detail.value.emailPwd;
      if (!emailPwd) {
        wx.showToast({
          title: '邮箱密码不能为空',
          icon: 'none'
        })
        return;
      }
    }
    let remark = e.detail.value.remark;
    that.setData({
      isSubmit: true
    })
    customRequst.postRequest('api/contribution/create', {
      id: that.data.periId,
      is_statement: that.data.statement,
      token: that.data.Formtoken,
      file_path: that.data.uploadFileObj.url,
      works_name: works_name,
      author: author,
      remark: remark,
      email: email,
      email_psd: emailPwd,
    }).then(res => {
      if (res.code == 1) {
        //清理文件
        app.globalData.uploadFileObj = "";
        wx.showToast({
          title: '订单创建成功',
          icon: 'success',
          duration: 2000,
          mask: true
        });
        setTimeout(function () {
          if (that.data.periInfo.types_id == 1) {
            wx.navigateTo({
              url: '/pages/order/list/index',
            })
          } else {
            wx.navigateTo({
              url: '/pages/order/list/index?type=1',
            })
          }
        }, 2000);
      } else if (res.code == 4) {
        wx.showToast({
          title: res.msg,
        })
        that.setData({
          Formtoken: res.data.token,
          isSubmit: false,
        })
      } else {
        wx.showToast({
          title: res.msg,
          success: function () {
            that.setData({
              Formtoken: res.data.token,
              isSubmit: false,
            })
          }
        })
      }
    })
  },
  // 打开遮盖层
  openCover() {
    let that = this
    that.setData({
      isShow: true
    })
  },
  // 关闭遮盖层
  close() {
    let that = this
    that.setData({
      isShow: false
    })
  },
})